/*
 *
 * Licensed to the Apache Software Foundation (ASF) under one
 * or more contributor license agreements.  See the NOTICE file
 * distributed with this work for additional information
 * regarding copyright ownership.  The ASF licenses this file
 * to you under the Apache License, Version 2.0 (the
 * "License"); you may not use this file except in compliance
 * with the License.  You may obtain a copy of the License at
 *
 *   http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing,
 * software distributed under the License is distributed on an
 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
 * KIND, either express or implied.  See the License for the
 * specific language governing permissions and limitations
 * under the License.
 *
 */
package org.apache.airavata.metcat;

import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import java.util.Properties;

public class ServerProperties {
	
	private static ServerProperties props = null;
	
	private static final String SERVER_PROPERTY_FILE = "../conf/msgBrokerMonitor.properties";
	
    private static final String BROKER_URL = "messagebroker.url";
    private static final String LISTEN_PORT = "metcatserver.port";
    
    private String brokerURL = null;
    private int listenPort = -1;
    
    private ServerProperties() {
    	this.loadMessageBrokerProperties();
    }
    
    public static ServerProperties getInstance() {
    	if (props == null) {
    		props = new ServerProperties();
    	}
    	return props;
    }
    
    private InputStream getMessageBrokerPropertiesURL() {
        InputStream file;
        //load msgBrokerMonitor properties from conf directory.
        try {
            file = new FileInputStream(SERVER_PROPERTY_FILE);
        } catch (FileNotFoundException e) {
            file = null;
            System.out.println("Using default msgBrokerMonitor.properties");
        }
        if(file==null)
        {   // try to load default msgBrokerMonitor.properties from class loader.
            try {
                file =  ClassLoader.getSystemResource("conf/msgBrokerMonitor.properties").openStream();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        return file;
    }
    
    private void loadMessageBrokerProperties() {
        Properties properties = new Properties();
        try {
            InputStream file = getMessageBrokerPropertiesURL();
            properties.load(file);
            brokerURL = (String) properties.get(BROKER_URL);
            listenPort = Integer.parseInt((String) properties.get(LISTEN_PORT));
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
    
    public String getBrokerURL() {
		return brokerURL;
	}
    
    public int getListenPort() {
		return listenPort;
	}
    
}
